package main

/*八皇后问题*/
import "fmt"

const (
	WEI = 4
)

var Count = 0
func NotDanger(r, c int,chess [][WEI]int) (flag bool){
	//判断列方向
	for i:=0;i<WEI;i++{
		if(chess[i][c]!=0){

			return
		}
	}
	//判断左上方
	for i,j:=r,c;i>=0 && j>=0; i,j=i-1,j-1{
            if(chess[i][j] !=0){
		    return
	    }
	}
	//判断右下方
	for i,j:=r,c;i<WEI && j<WEI; i,j=i+1,j+1{
		if(chess[i][j] !=0){
			return

		}
	}

	//判断右上方
	for i,j:=r,c;i>=0 && j<WEI; i,j=i-1,j+1{
		if(chess[i][j] !=0){
			return

		}
	}

	//判断左下方
	for i,j:=r,c;i<WEI && j>=0; i,j=i+1,j-1{
		if(chess[i][j] !=0){
			return
		}
	}

	return true

}
func EightQueen(r, c int, chess [][WEI]int) {
	tmp :=  make([][WEI]int, WEI)
	var i, j int
	for i = 0; i < WEI; i++ {
		for j = 0; j < WEI; j++ {
			tmp[i][j] = chess[i][j]
		}
	}

	if (r == WEI) {
		fmt.Printf("第%d种解法\n ",Count+1);
		for i = 0; i < WEI; i++ {
			for j = 0; j < WEI; j++ {
				fmt.Printf("%d ", tmp[i][j]);
			}
			fmt.Printf("\n ");
		}
		fmt.Printf("\n ");
		Count=Count+1
	}else {
		for j = 0; j < c; j++ {
			if(NotDanger(r,j,chess)){ //判断是否危险
				for i = 0; i < WEI; i++ {
					tmp[r][i] = 0
				}
				tmp[r][j]=1
				EightQueen(r+1,c,tmp);
			}
		}
	}
}
func main() {
	a := make([][WEI]int, WEI)

	fmt.Printf("%v\n", a)

	EightQueen(0, WEI, a)


}
